<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content=
"HTML Tidy for Linux/x86 (vers 1st November 2003), see www.w3.org" />
<meta http-equiv="Content-Type" content=
"text/html; charset=us-ascii" />
<title>4.&nbsp;Programming questions</title>
<link rel="stylesheet" href="faq.css" type="text/css" />
<meta name="generator" content="DocBook XSL Stylesheets V1.64.1" />
<link rel="home" href="index.html" title=
"Frequently Asked Questions for comp.lang.lisp" />
<link rel="up" href="index.html" title=
"Frequently Asked Questions for comp.lang.lisp" />
<link rel="previous" href="terminology.html" title=
"3.&nbsp;Terminology" />
<link rel="next" href="pitfalls.html" title=
"5.&nbsp;Common programming pitfalls" />
<link rel="article" href="index.html" title=
"Frequently Asked Questions for comp.lang.lisp" />
<!-- generated HTML; do not edit -->
</head>
<body>
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">4.&nbsp;Programming questions</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href=
"terminology.html">Prev</a>&nbsp;</td>
<th width="60%" align="center">&nbsp;</th>
<td width="20%" align="right">&nbsp;<a accesskey="n" href=
"pitfalls.html">Next</a></td>
</tr>
</table>
<hr /></div>
<div class="sect1" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id=
"programming"></a>4.&nbsp;Programming questions</h2>
</div>
</div>
</div>
<div class="qandaset">
<dl>
<dt>4.1. <a href="programming.html#id2519320">How do I write a
"Hello, World!" programme in Lisp?</a></dt>
<dt>4.2. <a href="programming.html#id2519343">How do I make an
executable from my programme?</a></dt>
<dt>4.3. <a href="programming.html#id2519929">How do I run my
programme as a script, then?</a></dt>
<dt>4.4. <a href="programming.html#id2520235">What is the Lisp
equivalent of the __FILE__ and __LINE__ ANSI C preprocessor macros?
How do I find out where an error occurred?</a></dt>
<dt>4.5. <a href="programming.html#id2520406">How do I split a
string?</a></dt>
<dt>4.6. <a href="programming.html#id2520503">Why doesn't Common
Lisp have continuations?</a></dt>
<dt>4.7. <a href="programming.html#id2520499">How does (defun car
(x) (car x)) work?</a></dt>
<dt>4.8. <a href="programming.html#id2520672">Does anyone actually
use FLET?</a></dt>
</dl>
<table border="0" summary="Q and A Set">
<col align="left" width="1%" />
<tbody>
<tr class="question">
<td align="left" valign="top"><a id="id2519320"></a><a id=
"id2519325"></a><b>4.1.</b></td>
<td align="left" valign="top">
<p>How do I write a "Hello, World!" programme in Lisp?</p>
</td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>This is actually a subtle question; not in the respect of
computing "Hello, World!", obviously, but because of what being a
"Hello, World!" programme actually means.</p>
<p>At its simplest, you can simply type "Hello, World!" at the
REPL, to find that the `P' part of that will print "Hello, World!"
back to you. However, this won't do what you want if this is in a
file, as return values aren't printed unless they are at the
REPL.</p>
<p>Something which is closer to the canonical "Hello, World!"
attempt is <b class="userinput"><tt>(write-line "Hello,
World!")</tt></b></p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top"><a id="id2519343"></a><a id=
"id2519345"></a><b>4.2.</b></td>
<td align="left" valign="top">
<p>How do I make an executable from my programme?</p>
</td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>This depends on your implementation; you will need to consult
your vendor's documentation.</p>
<div class="itemizedlist">
<ul type="disc">
<li>With ECL and GCL, the standard compilation process will produce
a native executable.</li>
<li>With <span class="productname">LispWorks</span>&trade;, see the
<span class="emphasis"><em>Delivery User's Guide</em></span>
section of the documentation.</li>
<li>With <span class="productname">Allegro Common
Lisp</span>&trade;, see the <span class=
"emphasis"><em>Delivery</em></span> section of the manual.</li>
</ul>
</div>
<p>However, the classical way of interacting with Common Lisp
programs does not involve standalone executables. Let's consider
this during two phases of the development process: programming and
delivery.</p>
<p><span class="bold"><b>Programming phase</b></span>: Common Lisp
development has more of an incremental feel than is common in
batch-oriented languages, where an edit-compile-link cycle is
common. A CL developer will run simple tests and transient
interactions with the environment at the <span class=
"acronym">REPL</span> (or Read-Eval-Print-Loop, also known as the
<span class="emphasis"><em>listener</em></span>). Source code is
saved in files, and the build/load dependencies between source
files are recorded in a system-description facility such as ASDF
(which plays a similar role to <span class=
"emphasis"><em>make</em></span> in edit-compile-link systems). The
system-description facility provides commands for building a system
(and only recompiling files whose dependencies have changed since
the last build), and for loading a system into memory.</p>
<p>Most Common Lisp implementations also provide a "save-world"
mechanism that makes it possible to save a snapshot of the current
lisp image, in a form which can later be restarted. A Common Lisp
environment generally consists of a relatively small executable
runtime, and a larger image file that contains the state of the
lisp world. A common use of this facility is to dump a customized
image containing all the build tools and libraries that are used on
a given project, in order to reduce startup time. For instance,
this facility is available under the name <tt class=
"function">EXT:SAVE-LISP</tt> in CMUCL, <tt class=
"function">SB-EXT:SAVE-LISP-AND-DIE</tt> in SBCL, <tt class=
"function">EXT:SAVEINITMEM</tt> in CLISP, and <tt class=
"function">CCL:SAVE-APPLICATION</tt> in OpenMCL. The</p>
<p><span class="bold"><b>Application delivery</b></span>: rather
than generating a single executable file for an application, Lisp
developers generally save an image containing their application,
and deliver it to clients together with the runtime and possibly a
shell-script wrapper that invokes the runtime with the application
image. On Windows platforms this can be hidden from the user by
using a click-o-matic <span class=
"productname">InstallShield</span>&trade; type tool.</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top"><a id="id2519929"></a><a id=
"id2520215"></a><b>4.3.</b></td>
<td align="left" valign="top">
<p>How do I run my programme as a script, then?</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top"><a id="id2520235"></a><a id=
"id2520260"></a><b>4.4.</b></td>
<td align="left" valign="top">
<p>What is the Lisp equivalent of the <span class=
"symbol">__FILE__</span> and <span class="symbol">__LINE__</span>
<span class="acronym">ANSI</span> C preprocessor macros? How do I
find out where an error occurred?</p>
</td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>There is no direct equivalent of <span class=
"symbol">__FILE__</span> and <span class="symbol">__LINE__</span>
in ANSI Common Lisp; this is perhaps most simply explained by the
fact that CL is not particularly a file-oriented and definitely not
a line-oriented language. That said, your particular implementation
may carry around some information about where functions were
compiled, and <tt class="code">COMPILE-FILE</tt> binds the special
variables <tt class="code">*COMPILE-FILE-TRUENAME*</tt> and
<tt class="code">*COMPILE-FILE-PATHNAME*</tt>.</p>
</td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<table border="0" bgcolor="#E0E0E0">
<tr>
<td>
<pre class="programlisting">
      (defun foo () (break "Stopped inside ~S" (the-function-i-am-in)))
      
      (setf (symbol-function 'bar) (symbol-function 'foo))
      
      (bar)
    
</pre></td>
</tr>
</table>
<p>Are you in a breakpoint in FOO or in BAR?</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top"><a id="id2520406"></a><a id=
"id2520551"></a><b>4.5.</b></td>
<td align="left" valign="top">
<p>How do I split a string?</p>
</td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>There is no `right' answer to this question; many lisp
programmers have rolled their own solution in the past, and others
are of the view that it should never be necessary, as long as all
sequence functions are used with consistent :start and :end
arguments.</p>
<p>However, a community-based `standard' was developed in June/July
2001 on <a href="news:comp.lang.lisp" target=
"_top">comp.lang.lisp</a>; known as SPLIT-SEQUENCE (formerly
PARTITION), it works as follows in its simplest form:</p>
<p><b class="userinput"><tt>(split-sequence #\Space "A stitch in
time saves nine.")</tt></b> gives <tt class="computeroutput">("A"
"stitch" "in" "time" "saves" "nine."), 28</tt></p>
<p>For more details, consult the specification, available from
<a href="http://ww.telent.net/cliki/SPLIT-SEQUENCE" target=
"_top">its CLiki page</a>.</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top"><a id="id2520503"></a><a id=
"id2520692"></a><b>4.6.</b></td>
<td align="left" valign="top">
<p>Why doesn't Common Lisp have continuations?</p>
</td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>Continuations are a great theoretical tool; if a language has
first-class, multiply invocable continuations then one can build
threads, exceptions, coroutines, and the kitchen sink on top.</p>
<p>However, there is an implementation burden with continuations;
supporting first-class, multiply invocable continuations
complicates things tremendously for the Lisp implementor. The ANSI
standardizing committee J13, mindful of this, took the view that it
would be better to specify the user-level control structure (CATCH,
UNWIND-PROTECT, and so on) and let implementors choose whether to
build those on top of continuations or not.</p>
<p>If you need to play with continuations, you should use a Scheme
implementation.</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top"><a id="id2520499"></a><a id=
"id2520615"></a><b>4.7.</b></td>
<td align="left" valign="top">
<p>How does <b class="userinput"><tt>(defun car (x) (car
x))</tt></b> work?</p>
</td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>This code is probably part of the source to a lisp compiler,
which knows how to open-code calls to CAR. However, the interpreter
also needs to know how to call CAR, which is what the above defun
is doing. This is not recommended in user code...</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top"><a id="id2520672"></a><a id=
"id2520674"></a><b>4.8.</b></td>
<td align="left" valign="top">
<p>Does anyone actually use <tt class="code">FLET</tt>?</p>
</td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>This question is usually motivated by the existence of
<tt class="code">LABELS</tt>, which is a similar form for binding
functions, but also allows mutual recursion between the functions
being bound. Given this, it is perhaps natural to question the
utility of <tt class="code">FLET</tt>.</p>
<p>However, there are two reasons for using flet: one idiomatic and
one programmatic. The idiomatic reason is that flet can be useful
to signal to the reader of the code that it is not expected that
the functions will be mutually recursive; in other words, it is
part of the documentation of the system, so that a subsequent
programmer can see by inspection the purpose of binding the
functions.</p>
<p>More usefully, though, flet can be useful to locally modify the
behaviour of functions, for instance (a contrived example due to
Kent Pitman):</p>
<table border="0" bgcolor="#E0E0E0">
<tr>
<td>
<pre class="programlisting">
      (defun square (x) (* x x))
      
      (flet ((square (x) (make-instance 'square :area (squarex)))) ...)
    
</pre></td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href=
"terminology.html">Prev</a>&nbsp;</td>
<td width="20%" align="center"><a accesskey="u" href=
"index.html">Up</a></td>
<td width="40%" align="right">&nbsp;<a accesskey="n" href=
"pitfalls.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">
3.&nbsp;Terminology&nbsp;</td>
<td width="20%" align="center"><a accesskey="h" href=
"index.html">Home</a></td>
<td width="40%" align="right" valign="top">&nbsp;5.&nbsp;Common
programming pitfalls</td>
</tr>
</table>
</div>
</body>
</html>
